// 单词模块
const {word,Result,Op,db} = require('../models');

async function addWord(ctx,next){
    let {chinese,english,classType='default'} = ctx.request.body;
    if(!chinese || !english){
        return ctx.body = new Result(507,ctx.request.body,'参数缺失',false);
    }
   
    let {id} = ctx.state.userInfo;

    //检查单词是否已经存在    
    let reExist = await word.findOne({where:{
        authorId:id,english
    }});

    if(reExist){
        return ctx.body = new Result(509,reExist,'单词已经存在',false);
    }
    
    let reCreate = await word.create({
        chinese,english,classType,authorId:id
    })

    if(reCreate){
        console.log("单词添加成功",);
        return ctx.body = new Result(200,reCreate,'单词添加成功',true);
    }
    ctx.body = new Result(508,reCreate,'单词添加失败',false);
}

async function getWord(ctx,next){
    let {id,chinese,english} = ctx.query;
    console.log("查询单词参数列表：",ctx.query);
    //动态获取参数
    let where = {};
    id && (where['id'] = id);
    chinese && (where['chinese'] = id);
    english && (where['english'] = id);

    let success = await word.findAll({
        where
    })

    if (success) {
        console.log("单词查询成功",success);
        return ctx.body = new Result(200,success.map(e=>e.dataValues),'单词查询成功',true);
    }

    console.log("单词查询失败");
    ctx.body = new Result(500,null,'单词查询失败',false);
    
    
    
}

async function updateWord(ctx,next){
    //单词更新
    let {chinese,english,id} = ctx.request.body;
    if (!chinese || !english || !id) {
        return ctx.body = new Result(500,null,'参数缺失',false);
    }
    

    let reExist = await word.findOne({where:{
        id
    }});
    if (!reExist) {
        return ctx.body = new Result(501,null,'单词不存在',false);
    }

    let reUpdate = await word.update({chinese,english},{where:{id}});
    if (reUpdate) {
        console.log("单词更新成功！",reUpdate);   
        return ctx.body = new Result(200,reUpdate,'单词更新成功',true);
    }
    ctx.body = new Result(500,null,'单词更新失败',false);
}

async function delWord(ctx,next){
    let {id} = ctx.params;
    let reDest = await word.destroy({where:{id}});
    if (reDest) {
        return ctx.body = new Result(200,reDest,'单词删除成功',true);
    }
    ctx.body = new Result(510,reDest,'单词删除失败',false);
}

//模糊查询单词，不支持id查找
async function likeWord(ctx,next){
    let {str} = ctx.query;
    if(!str) return ctx.body = new Result(511,str,'缺少关键词',false);
    await word.findAll({where:{[Op.or]:{
        chinese:{[Op.substring]:str},
        english:{[Op.substring]:str}
    }}}).then(success=>{
        console.log("模糊查询成功",success);
        return ctx.body = new Result(200,success.map(e=>e.dataValues),'模糊查询成功',true);
    }).catch(err=>{
        console.log("模糊查询失败");
        return ctx.body = new Result(512,err,'模糊查询失败',false);
    })
}

async function getAllType(ctx,next){
    let {me} = ctx.query;
    //获取所有类型
    let reCl = await db.query(`select distinct classType from words ${me?'where authorId = '+ctx.state.userInfo.id:''}`);
    if (reCl) {
        return ctx.body = new Result(200,reCl[0],'所有类型查询成功',true);
    }
    return ctx.body = new Result(513,null,'所有类型查询失败',false);
}

module.exports = {
    'post /word':addWord,
    'get /word':getWord,
    'put /word':updateWord,
    'delete /word/:id':delWord,
    'get /likeword':likeWord,
    'get /type':getAllType
}